package com.lzhpo.admin.controller.base;

import com.alibaba.fastjson.JSON;
import com.lzhpo.admin.entity.vo.SelectCondition;
import com.lzhpo.common.config.MySysUser;
import com.lzhpo.core.config.RedisUtil;
import com.lzhpo.core.utils.CalculateFiveUtil;
import com.lzhpo.core.utils.CalculateUtil;
import com.lzhpo.core.utils.ConditionEnum;
import com.lzhpo.core.utils.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Set;
import java.util.UUID;

public abstract class BaseController {

    public static  final String DEFAULT_SPERATORCHAR=";";


    @Autowired
    private RedisUtil redisUtil;



    public <T> ModelAndView  buildModeAndView(List<T> list,
                                              List<String[]> bottomStat,
                                              String viewName
    ){
        ModelAndView mv = new ModelAndView();
        mv.addObject("lists", list);
        mv.addObject("statics", bottomStat);
        mv.setViewName(viewName);
        return  mv;
    }


    public void saveBeijingBoatDataToRedis(SelectCondition condition){

        redisUtil.hset(RedisConstant.USER_UUID_SET + MySysUser.id(),
                condition.getId(),
                JSON.toJSONString(condition.getResult()));

        redisUtil.hset(RedisConstant.USER_CONDITION + MySysUser.id(),
                condition.getId(),
                JSON.toJSONString(condition));


        redisUtil.hset(RedisConstant.USER_CONDITION_INFO + MySysUser.id(),
                condition.getId(),
                condition.getFormData());

    }

    public void saveRangeFiveDataToRedis(SelectCondition condition){

        redisUtil.hset(RedisConstant.USER_UUID_SET_FIVE + MySysUser.id(),
                condition.getId(),
                JSON.toJSONString(condition.getResult()));

        redisUtil.hset(RedisConstant.USER_CONDITION_FIVE + MySysUser.id(),
                condition.getId(),
                JSON.toJSONString(condition));

        redisUtil.hset(RedisConstant.USER_CONDITION_INFO_FIVE + MySysUser.id(),
                condition.getId(),
                condition.getFormData());

    }


    public String delStringBufferLastChar(StringBuffer buffer){
        return delStringBufferLastChar(buffer,DEFAULT_SPERATORCHAR);
    }

    public String delStringBufferLastChar(StringBuffer buffer,String chars){
        String textDesc = buffer.toString();
        if (textDesc.endsWith(chars)) {
            textDesc = textDesc.substring(0, textDesc.length() - 1);
        }
        return  textDesc;
    }


    public SelectCondition saveTrendRegionDataToRedis(List<Integer> region,
                                                       List<Integer> regionOccurs,
                                                       String uuid, int type) {

        String conditionId = "";
        if (org.apache.commons.lang3.StringUtils.isBlank(uuid)) {
            conditionId = UUID.randomUUID().toString();
        } else {
            conditionId = uuid;
        }
        Set<String> danma = null;
        if (type == 2) {
            danma = CalculateUtil.calcDanMa(region, regionOccurs);
        }
        if (type == 3) {
            danma = CalculateFiveUtil.calcDanMa(region, regionOccurs);
        }
        SelectCondition condition = new SelectCondition();
        StringBuffer buffer = new StringBuffer();
        condition.setType(ConditionEnum.DANMA.getLabel());
        condition.setCount(danma.size());
        condition.setId(conditionId);
        buffer.append(org.springframework.util.StringUtils.collectionToCommaDelimitedString(region));
        buffer.append("  出");
        buffer.append(StringUtils.collectionToCommaDelimitedString(regionOccurs));
        condition.setContent(buffer.toString());
        buffer = new StringBuffer();
        buffer.append(ConditionEnum.DANMA.getIndex());
        buffer.append(";");
        buffer.append(StringUtils.collectionToCommaDelimitedString(region));
        buffer.append(";");
        buffer.append(StringUtils.collectionToCommaDelimitedString(regionOccurs));
        condition.setFormData(buffer.toString());
        condition.setResult(danma);
        //将id对应的数据集合放入redis
        if (type == 2) {
            saveBeijingBoatDataToRedis(condition);
        }
        if (type == 3) {
            saveRangeFiveDataToRedis(condition);
        }
        return condition;
    }


}
